package com.atosworldline.tfm.controller;


import java.lang.reflect.Method;

import javax.inject.Inject;
import javax.inject.Named;

import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import com.sun.jersey.api.NotFoundException;

import org.resthub.web.controller.GenericControllerImpl;

import com.atosworldline.tfm.model.Document;
import com.atosworldline.tfm.service.DocumentService;
import com.atosworldline.tfm.classLoader.DynamicClassLoader;

import org.codehaus.jackson.map.ObjectMapper;

@Path("/doccontroller")
@Named("documentController")
public class DocumentController extends GenericControllerImpl<Document, Long, DocumentService> {

	@Inject
		@Named("documentService")
		@Override
		public void setService(DocumentService service) {
			this.service = service;
		}

	private String packageName(){
		String packageName = this.getClass().getPackage().getName();
		return packageName.substring(0,packageName.lastIndexOf(".")+1)+"model";
	}


	private String fullClassName(String classParam){
		return this.packageName()+"."+classParam;
	}
	private Class requestClass(String classParam) throws ClassNotFoundException{
	
	 	ClassLoader parentClassLoader = DynamicClassLoader.class.getClassLoader();
    		DynamicClassLoader classLoader = new DynamicClassLoader(parentClassLoader);	

		return classLoader.loadClass(this.packageName(), classParam);	
	}

	@GET
		@Path("/object/{pClass}/{id}")
		@Produces("application/json")
		public HashMap classController(@PathParam("pClass") String pClass, @PathParam("id") Long id ) {

			try {
				Class theClass  = this.requestClass(pClass);
			}catch ( ClassNotFoundException ex ){
				System.err.println( ex + " The given class "+pClass+" must be in class path.");
			}
			Document doc =  this.service.findById(id);
			if(doc==null){return null;}
			if(!doc.getClass().getName().equals(this.fullClassName(pClass))){
				System.out.println("object nofound");	
				throw new NotFoundException();
			}

			return doc.getParameter();
		}	

	@POST
		@Consumes("application/json")
		@Path("/object/{pClass}/")
		public String classController(@PathParam("pClass") String pClass, String message){
			Document object=null;		
			String toReturn="0\n";
				try {

				Class theClass  = this.requestClass(pClass);

				ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
				object = (Document) mapper.readValue(message, theClass);

				object = service.create(object);
				toReturn=object.getId()+"\n";			
	}
			catch ( ClassNotFoundException ex ){
				System.err.println( ex + " The given class "+pClass+" must be in class path.");
			}
			catch( Exception ex){
				System.out.println(" Something else append : "+ex);
				ex.printStackTrace();
			}

			return toReturn;
		}

	@PUT
		@Consumes("application/json")
		@Path("/object/{pClass}/{id}")
		public String classupdateController(@PathParam("pClass") String pClass,@PathParam("id") Long id, String message){
			Long toReturn=0L;
			try {

				Class theClass  = this.requestClass(pClass);

				ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
				Document object = (Document) mapper.readValue(message, theClass);
				object.setId(id);	
				object=service.update(object);

				toReturn= object.getId();
			}
			catch ( ClassNotFoundException ex ){
				System.err.println( ex + " The given class "+pClass+"must be in class path.");
			}
			catch( Exception ex){
				System.out.println(" Something else append : "+ex);
				ex.printStackTrace();
			}

			return toReturn.toString();
		}
}
